﻿using System;
using System.Collections.Generic;
using System.IO.MemoryMappedFiles;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp10
{
    class SystemIOClasses
    {
        public void Show()
        {
            /*  using (Stream s = new FileStream("test.txt", FileMode.Create))
              {
                  Console.WriteLine($"CanRead:{s.CanRead}");
                  Console.WriteLine($"CanSeek:{s.CanSeek}");
                  Console.WriteLine($"CanWrite:{s.CanWrite}");

                  s.WriteByte(101);
                  s.WriteByte(102);
                  byte[] block = { 1, 2, 3, 4, 5 };
                  s.Write(block, 0, block.Length);

                  Console.WriteLine($"Length:{s.Length}");
                  Console.WriteLine($"Position:{s.Position}");
                  s.Position = 0;

                  Console.WriteLine(s.ReadByte());
                  Console.WriteLine(s.ReadByte());
                  //Console.WriteLine(s.ReadByte());

                  // 
                  Console.WriteLine($"BlockLength:{s.Read(block, 0, block.Length)}");

                  Console.WriteLine();

                  Console.WriteLine($"BlockLength:{s.Read(block, 0, block.Length)}");

              }*/

            /* //AsyncDemo();

         FileStream fs = File.OpenRead("readme.bin");
           FileStream fs2 = File.OpenWrite("writeme.tmp"); // 可以保留文件中的原来的内容
           FileStream fs3 = File.Create("readWrite.tmp"); // 覆盖写

           using var fs33 = new FileStream("readwrite.tmp", FileMode.Open); // 开启了一个可读可写的文件流
           //Environment.CurrentDirectory=""
           //AppDomain.CurrentDomain.BaseDirectory  // 获取文件可执行的路径
           string baseFolder = AppDomain.CurrentDomain.BaseDirectory;
           string logoPath = Path.Combine(baseFolder, "logo.jpg");
           Console.WriteLine(File.Exists(logoPath));

           //File.ReadAllBytes("") // 返回一个byte数组
           File.ReadAllText(""); // 返回一个字符串
           File.ReadAllLines(""); // 返回一个字符串数组
           //File.WriteAllText()
           //File.WriteAllLines();
           //File.WriteAllBytes();
           //File.AppendAllLines
           //File.AppendAllText();
           //ReadLine 方法不是一次性把内容都读取到内存中，可以使用Linq方式去消费它的内容
           int longLines = File.ReadLines("filePath").Count(l => l.Length > 80);

           //File.OpenRead("") 等价于 FileMode.Open

          using var fs = new FileStream("myFile.bin", FileMode.Open);
          fs.Seek(0, SeekOrigin.End);*/

            //var ms = new MemoryStream();
            //var arr = ms.ToArray();
            //var buff = ms.GetBuffer();
            //var ps = new PipeStream();
            //AnonymousPipeServerStream
            //AnonymousPipeClientStream
            //NamedPipeClientStream
            //NamedPipeServerStream

            //NamedPipeServerStream namedPipeServerStream = new NamedPipeServerStream("");
            //namedPipeServerStream.WaitForConnection();
            //NamedPipeClientStream namedPipeClientStream = new NamedPipeClientStream("");
            //namedPipeClientStream.Connect();

            /*  using var s = new NamedPipeServerStream("pipedream");
              s.WaitForConnection();
              s.WriteByte(100);
              Console.WriteLine(s.ReadByte());

              //using var c = new NamedPipeClientStream("pipedream");
              //c.Connect();
              //Console.WriteLine(c.ReadByte());
              //c.WriteByte(200);*/

            /*    using var stream = new NamedPipeServerStream("pipedream", PipeDirection.InOut, 1, PipeTransmissionMode.Message);
                stream.WaitForConnection();
                byte[] msg = Encoding.UTF8.GetBytes("hello");
                stream.Write(msg, 0, msg.Length);
                Console.WriteLine(Encoding.UTF8.GetString(ReadMessage(stream)));

                var anony = new AnonymousPipeServerStream();*/

            /*     File.WriteAllBytes("myFile.bin", new byte[100000]);
                 using FileStream fs = File.OpenRead("myFile.bin");
                 using BufferedStream bs = new BufferedStream(fs, 20000); // 20K buffer
                 bs.ReadByte();
                 Console.WriteLine(fs.Position); // 20000*/

            // StreamReader  and  StreamWriter
            /* using(FileStream fs = File.Create("test.txt"))
             using (TextWriter writer = new StreamWriter(fs))
             {
                 writer.WriteLine("Line1");
                 writer.WriteLine("Line2");
             }

             using (FileStream fs = File.OpenRead("test.txt"))
             using (TextReader reader = new StreamReader(fs))
             {
                 Console.WriteLine(reader.ReadLine()); // Line1
                 Console.WriteLine(reader.ReadLine()); // Line2
             }*/

            // 创建文件并写入内容
            /* using(TextWriter writer = File.CreateText("text.txt"))
             {
                 writer.WriteLine("New Line1");
                 writer.WriteLine("New Line2");
             }
             // 往现有文件中追加内容
             using (TextWriter writer = File.AppendText("text.txt"))
                 writer.WriteLine("New Line3");
             // 读取文件内容
             using (TextReader reader = File.OpenText("text.txt"))
                 while(reader.Peek()>-1)
                     Console.WriteLine(reader.ReadLine());*/

            /*   using (TextWriter w = File.CreateText("data.txt"))
               {
                   w.WriteLine(123);  // writes 123
                   w.WriteLine(true); // wirtes the word "true"
               }
               using (TextReader r = File.OpenText("data.txt"))
               {
                   int myInt = int.Parse(r.ReadLine());   // myInt == 123
                   bool yes = bool.Parse(r.ReadLine()); // yes == true
                   Console.WriteLine(myInt==123);
                   Console.WriteLine(yes==true);
               }*/


            /*using(TextWriter w = File.CreateText("but.txt"))
            {
                w.WriteLine("but-");
            }

            using(Stream s = File.OpenRead("but.txt"))
                for(int b;(b=s.ReadByte())>-1;)
                    Console.WriteLine(b);*/

            using (Stream s = File.Create("but.txt"))
            using (TextWriter w = new StreamWriter(s, Encoding.Unicode))
                w.WriteLine("but-");
            foreach (byte b in File.ReadAllBytes("but.txt"))
            {
                Console.WriteLine(b);
            }


        }

        async void AsyncDemo()
        {
            using (Stream s = new FileStream("test.txt", FileMode.Create))
            {
                //byte[] block = { 1, 2, 3, 4, 5 };
                //await s.WriteAsync(block, 0, block.Length);
                //s.Position = 0;
                //Console.WriteLine(await s.ReadAsync(block,0,block.Length));

                //byte[] data = new byte[1000];
                //int bytesRead = 0;
                //int chunkSize = 1;
                //while (bytesRead < data.Length && chunkSize > 0)
                //    bytesRead += chunkSize = s.Read(data, bytesRead, data.Length - bytesRead);

                byte[] data = new BinaryReader(s).ReadBytes(1000);
                //MemoryMappedFile
                s.ReadTimeout = 3000;
                s.WriteTimeout = 4000;
                //RandomAccess

            }
        }

        byte[] ReadMessage(PipeStream stream)
        {
            MemoryStream ms = new MemoryStream();
            byte[] buffer = new byte[0x1000];  // Read in 4kB blocks
            do
            {
                ms.Write(buffer, 0, stream.Read(buffer, 0, buffer.Length));
            } while (!stream.IsMessageComplete);
            return ms.ToArray();
        }

    }


}
